<?php

namespace App\ApiBackend\User;

use App\Domains\UserDao;
use App\Http\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

/**
 *  用户密码
 */
class Password extends ApiBaseController
{


  /**
   * 参数检查
   */
  protected function check(): void
  {
    try {
      request()->validate([
        'userId' => 'required',
        'password' => 'required',
      ]);
    } catch (\Exception $exception) {
      $this->result["code"] = 1;
      $this->result["message"] = "预检: " . $exception->getMessage();
    }
  }

  /**
   * 业务主体
   */
  protected function service(): void
  {
    //数据
    $updateData = [
      "password" => Hash::make(\request('password')),
    ];
    //更新数据
    try {
      DB::table('user')->where('userId', request('userId'))->update($updateData);
      $this->result["message"] = "修改密码成功";
    } catch (\Exception $exception) {
      $this->result["code"] = 2;
      $this->result["message"] = $exception->getMessage();
    }
    return;
  }
}
